using System;
using System.Data;
using System.Text;

namespace FreeFlow.Administration
{
	/// <summary>
	/// An entry in the designer log.
	/// </summary>
	public class DesignerLogEntry : DatabaseAware, IDeletable
	{
    private int logId;
		internal DesignerLogEntry(Server server, IDataReader reader) : base(server)
		{
      description = reader["eDescription"].ToString();
      preEvaluation = reader["ePreEval"].ToString();
      postEvaluation = reader["ePostEval"].ToString();
      procedureName = reader["eProcedureName"].ToString();
      mapName = reader["eMapName"].ToString();
      stageName = reader["eStageName"].ToString();
      actionName = reader["eActionName"].ToString();
      details = reader["eDetails"].ToString();
      time = (DateTime)reader["eTime"];
      folderId = reader["eFolderId"].ToString();
      source = reader["eComponentType"].ToString();
      property = reader["ePropertyName"].ToString();
      userName = reader["eUserName"].ToString();
      formName = reader["eFormName"].ToString();
      fieldName = reader["eFieldName"].ToString();
      logId = Convert.ToInt32(reader["eLogID"]);
      detectedByClass = reader["eDetectedByClass"].ToString();
      detectedByMethod = reader["eDetectedByMethod"].ToString();
		}

    /// <summary>
    /// Returns a <see cref="T:System.String"/> that represents the designer log entry.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.String"/> that represents the current designer log entry.
    /// </returns>
    public override string ToString()
    {
      StringBuilder builder = new StringBuilder();
      builder.AppendFormat("Time: {0}{1}", time, Environment.NewLine);
      builder.AppendFormat("Procedure: {0}{1}", procedureName, Environment.NewLine);
      builder.AppendFormat("Map: {0}{1}", mapName, Environment.NewLine);
      builder.AppendFormat("Stage: {0}{1}", stageName, Environment.NewLine);
      builder.AppendFormat("Action: {0}{1}", actionName, Environment.NewLine);
      builder.AppendFormat("Folder ID: {0}{1}", folderId, Environment.NewLine);
      builder.AppendFormat("Source: {0}{1}", source, Environment.NewLine);
      builder.AppendFormat("Property: {0}{1}", property, Environment.NewLine);
      builder.AppendFormat("User: {0}{1}", userName, Environment.NewLine);
      builder.AppendFormat("Form: {0}{1}", formName, Environment.NewLine);
      builder.AppendFormat("Field: {0}{1}", fieldName, Environment.NewLine);
      builder.AppendFormat("Detected by class: {0}{1}", detectedByClass, Environment.NewLine);
      builder.AppendFormat("Detected by method: {0}{1}{1}", detectedByMethod, Environment.NewLine);
      builder.AppendFormat("Description: {0}{1}{1}", description, Environment.NewLine);
      builder.AppendFormat("Pre-evaluation: {0}{1}{1}", preEvaluation, Environment.NewLine);
      builder.AppendFormat("Post-evaluation: {0}{1}{1}", postEvaluation, Environment.NewLine);
      builder.AppendFormat("Details: {0}{1}", details, Environment.NewLine);
      return builder.ToString();
    }

    private string description;
    /// <summary>
    /// Gets the description for the entry.
    /// </summary>
    public string Description
    {
      get
      {
        return description;
      }
    }

    private string preEvaluation;
    /// <summary>
    /// Gets the formula in error prior to it being evaluated by the engine.
    /// </summary>
    public string PreEvaluation
    {
      get
      {
        return preEvaluation;
      }
    }

    private string postEvaluation;
    /// <summary>
    /// Gets the formula in error after it has been evaluated by the engine.
    /// </summary>
    public string PostEvaluation
    {
      get
      {
        return postEvaluation;
      }
    }

    private string folderId;
    /// <summary>
    /// Gets the ID of the folder in which the event occurred.
    /// </summary>
    public string FolderId
    {
      get
      {
        return folderId;
      }
    }

    private Folder folder;
    /// <summary>
    /// Gets the folder in which the event occurred.
    /// </summary>
    public Folder GetFolder()
    {
      if (folder == null)
        folder = Server.GetFolder(folderId);
      return folder;
    }

    private string procedureName;
    /// <summary>
    /// Gets the name of the procedure in which the event occurred.
    /// </summary>
    public string ProcedureName
    {
      get
      {
        return procedureName;
      }
    }

    private string mapName;
    /// <summary>
    /// Gets the name of the map in which the event occurred.
    /// </summary>
    public string MapName
    {
      get
      {
        return mapName;
      }
    }

    private string stageName;
    /// <summary>
    /// Gets the name of the stage in which the event occurred.
    /// </summary>
    public string StageName
    {
      get
      {
        return stageName;
      }
    }

    private string actionName;
    /// <summary>
    /// Gets the name of the action in which the event occurred.
    /// </summary>
    public string ActionName
    {
      get
      {
        return actionName;
      }
    }

    private string details;
    /// <summary>
    /// Gets the details of the event.
    /// </summary>
    public string Details
    {
      get
      {
        return details;
      }
    }

    private DateTime time;
    /// <summary>
    /// Gets the time that the event occurred.
    /// </summary>
    public DateTime Time
    {
      get
      {
        return time;
      }
    }

    private string source;
    /// <summary>
    /// Gets the source of the event.
    /// </summary>
    public string Source
    {
      get
      {
        return source;
      }
    }

    private string property;
    /// <summary>
    /// Gets the property associated with the this event.
    /// </summary>
    public string Property
    {
      get
      {
        return property;
      }
    }

    private string userName;
    /// <summary>
    /// Gets the name of the user that caused this event.
    /// </summary>
    public string UserName
    {
      get
      {
        return userName;
      }
    }

    private string formName;
    /// <summary>
    /// Gets the name of the form that caused this entry.
    /// </summary>
    public string FormName
    {
      get
      {
        return formName;
      }
    }

    private string fieldName;
    /// <summary>
    /// Gets the name of the field that caused this entry.
    /// </summary>
    public string FieldName
    {
      get
      {
        return fieldName;
      }
    }

    private string detectedByClass;
    /// <summary>
    /// Gets the name of the class where the error was detected.
    /// </summary>
    public string DetectedByClass
    {
      get
      {
        return detectedByClass;
      }
    }

    private string detectedByMethod;
    /// <summary>
    /// Gets the name of the method where the error was detected.
    /// </summary>
    public string DetectedByMethod
    {
      get
      {
        return detectedByMethod;
      }
    }

    #region IDeletable Members

    /// <summary>
    /// Deletes the log entry from the database.
    /// </summary>
    public void Delete()
    {
      Server.ExecuteNonQuery(string.Format("DELETE FROM eLog WHERE eLogID={0}", logId));
    }

    #endregion
  }
}
